package com.sweets.look.ui;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

/**
 * Author   Shone
 * Date     23/08/16.
 * Github   https://github.com/shonegg
 */
public class BaseActivity extends AppCompatActivity {

    private String ACTION_EXIT = "action_app_exit";
    private ExitReceiver mExitReceiver;

    private class ExitReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (ACTION_EXIT.equals(intent.getAction())) {
                BaseActivity.this.finish();
            }
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mExitReceiver = new ExitReceiver();
        IntentFilter filter = new IntentFilter(ACTION_EXIT);
        registerReceiver(mExitReceiver, filter);
    }


    @Override
    protected void onDestroy() {
        if (mExitReceiver != null) {
            unregisterReceiver(mExitReceiver);
            mExitReceiver = null;
        }
        super.onDestroy();
    }

    protected void sendExitAppBroadcast() {
        sendBroadcast(new Intent(ACTION_EXIT));
    }
}
